package com.cloud.mall.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.model.user.LoginAppUser;
import com.cloud.mall.model.user.SysRole;
import com.cloud.mall.model.utils.AppUserUtil;
import com.cloud.mall.ums.mapper.UmsAdminMapper;
import com.cloud.mall.ums.mapper.UmsMemberMapper;
import com.cloud.mall.ums.model.*;
import com.cloud.mall.user.dao.UmsRoleDao;
import com.cloud.mall.usr.service.AppUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class AppUserServiceImpl  implements AppUserService {
    @Autowired
    private UmsAdminMapper adminMapper;
    @Autowired
    private UmsMemberMapper memberMapper;
    @Autowired
    private UmsRoleDao roleDao;
    @Override
    public BaseResponse<LoginAppUser> findByUsername(String username) {
        String[] params = username.split("\\|");
        username = params[0];
        LoginAppUser loginAppUser = new LoginAppUser();
        if( params[1].equals("WEB")){
            UmsMember member=getMember(username);
            BeanUtils.copyProperties(member, loginAppUser);
            return BaseResponse.setResultSuccess(loginAppUser);
        }
        UmsAdminExample  example=new UmsAdminExample();
        if(StringUtils.isNotBlank(username)){
            example.createCriteria().andUsernameEqualTo(username);
        }
        List<UmsAdmin> adminList= adminMapper.selectByExample(example);
        if(CollectionUtil.isEmpty(adminList)){
           return  BaseResponse.setResultError();
        }
        UmsAdmin admin=adminList.get(0);
        BeanUtils.copyProperties(admin, loginAppUser);
        Set<UmsRole> roleSet = roleDao.getRoleList(admin.getId());
        Set<SysRole> sysRoleSet=new HashSet<>();
        for(UmsRole role:roleSet){
            SysRole sysRole=new SysRole();
            BeanUtils.copyProperties(role, sysRole);
            sysRoleSet.add(sysRole);
        }
        loginAppUser.setSysRoles(sysRoleSet);// 设置角色
        if (!CollectionUtils.isEmpty(sysRoleSet)) {
            Set<Long> roleIds = sysRoleSet.parallelStream().map(SysRole::getId).collect(Collectors.toSet());
           /* Set<SysPermission> sysPermissions = sysPermissionService.findByRoleIds(roleIds);
            if (!CollectionUtils.isEmpty(sysPermissions)) {
                Set<String> permissions = sysPermissions.parallelStream().map(SysPermission::getPermission)
                        .collect(Collectors.toSet());
                loginAppUser.setPermissions(permissions);// 设置权限集合
            }*/
        }
        return BaseResponse.setResultSuccess(loginAppUser);
    }

    @Override
    public BaseResponse<LoginAppUser> getLoginAppUser() {
        return BaseResponse.setResultSuccess(AppUserUtil.getLoginAppUser());
    }

    private  UmsMember getMember(String username){
        UmsMemberExample example=new  UmsMemberExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<UmsMember> memberList= memberMapper.selectByExample(example);
        return  memberList.size()>0?memberList.get(0):null;
    }
}
